/**
  ******************************************************************************
  * File Name          : sht30.c
  * Description        : This file provides code for the configuration
  *                      of the lora e22-400t22s instances.
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */
#include "tskcfg.h"
#include "bsp.h"
#include "sensor.h"
#include "edct.h"
#include "si2c_comm.h"

/* T&H Sensor task */
TaskHandle_t xSensorMain_Thd = NULL;
StaticTask_t xSensorMain_Tcb;
StackType_t  xSensorMain_Stk[ SENSORMAIN_STACK_SIZE ];
void vSensorMain_Task( void * pvParameters );

volatile uint8_t SHT30_FailCount = 0;

#ifndef EDCT_JUST_PRESS
typedef struct
{
	volatile float temperature;
	volatile float humidity;
}Sht30_DataType;

static uint8_t sht30_check_rational(float temperature, float humidity)
{
	if((temperature < -40.0) || (temperature > 120.0))
	{
		return 1;
	}
	
	if((humidity < 0.0) || (humidity > 120.0))
	{
		return 2;
	}

	return 0;
}

static void sht30_measure(void)
{
	uint8_t err;
	uint8_t active_sht30 = 0;
	volatile Sht30_DataType sht30_data;

	err = Sht30_GetSingleMeasure(&sht30_data.temperature, &sht30_data.humidity);
	if(err == 0)
	{
		active_sht30 = 1;
    SHT30_FailCount = 0;
	}
	else
	{
		active_sht30 = 0;
		SHT30_FailCount++;
	}
	
	if((active_sht30 != 0) && (sht30_check_rational(sht30_data.temperature, sht30_data.humidity) == 0))
	{
		SENSOR.Status_T    = 1;
		SENSOR.Status_H    = 1;
		SENSOR.temperature = sht30_data.temperature;
		SENSOR.humidity    = sht30_data.humidity;
    
    LED3_Toggle();
	}
	else
	{
		SENSOR.Status_T = 0.0;
		SENSOR.Status_H = 0.0;
    LED3(1);
	}
  
	if((SHT30_FailCount == 10) || (SHT30_FailCount == 15) || (SHT30_FailCount == 20))
	{
    Sht30_ResetCmd();
  }
  else if((SHT30_FailCount == 25) || (SHT30_FailCount == 30) || (SHT30_FailCount == 35))
  {
    SENSOR.Status_T    = 0;
		SENSOR.Status_H    = 0;
		SENSOR.temperature = 0.0;
		SENSOR.humidity    = 0.0;
    
    sI2C_ResetI2C();
  }
  else if((SHT30_FailCount > 35) && (SHT30_FailCount < 61) && ((SHT30_FailCount % 5) == 0))
  {
    SENSOR.Status_T    = 0;
		SENSOR.Status_H    = 0;
		SENSOR.temperature = 0.0;
		SENSOR.humidity    = 0.0;
    
    sI2c_IOPowerReset();
  }
  else if(SHT30_FailCount > 120)
  {
    SENSOR.Status_T    = 0;
		SENSOR.Status_H    = 0;
		SENSOR.temperature = 0.0;
		SENSOR.humidity    = 0.0;
    
    SHT30_FailCount = 1;
  }
}
#endif

void vSensorMain_Task( void * pvParameters )
{
  TickType_t xLastWakeTime;

#ifndef EDCT_JUST_PRESS
  xNH3CO2_Thd = xTaskCreateStatic(vNH3CO2_Task,
                                  "NH3",
                                  NH3CO2_STACK_SIZE,
                                  (void *)1,
                                  NH3CO2_TSK_PRIORITY,
                                  xNH3CO2_Stk,
                                  &xNH3CO2_Tcb);
                                  
  xSgp30_Thd = xTaskCreateStatic(vSgp30_Task,
                                 "gp30",
                                 SGP30_STACK_SIZE,
                                 (void *)1,
                                 SGP30_TSK_PRIORITY,
                                 xSgp30_Stk,
                                 &xSgp30_Tcb);
#endif
  
	xSdp810_Thd = xTaskCreateStatic(vSdp810_Task,
                                  "dp81",
                                  SDP810_STACK_SIZE,
                                  (void *)1,
                                  SDP810_TSK_PRIORITY,
                                  xSdp810_Stk,
                                  &xSdp810_Tcb);
  sI2c_IOPowerReset();
  
  xLastWakeTime = xTaskGetTickCount();
  
	while(1)
	{
    if(xTaskDelayUntil(&xLastWakeTime, 1000) == pdFALSE)
    {
      vTaskDelay(1000);
      xLastWakeTime = xTaskGetTickCount();
    }
    
#ifndef EDCT_JUST_PRESS    
		sht30_measure();
#endif
	}
}
